<template>
	<div class="layout-padding">
		<div class="layout-padding-auto layout-padding-view">
			<el-row>
				<div class="flex justify-between w-full mb8">
					<el-space>
						<el-popover trigger="click" :width="320" placement="bottom-start">
							<el-form :inline="false" :model="state.queryForm" @keyup.enter="getDataList" ref="queryRef" label-width="80px" size="small">
								<el-row>
									<!-- <el-col :span="24" class="mb20">
										<el-form-item label="设备型号" prop="deviceModelName">
											<el-input v-model="state.queryForm.deviceModelName" :placeholder="'请输入设备型号'" clearable />
										</el-form-item>
									</el-col> -->
									<el-col :span="24" class="mb20">
										<el-form-item label="设备类型" prop="deviceTypeId">
											<el-select v-model="state.queryForm.deviceTypeId" clearable :teleported="false" filterable placeholder="请选择设备类型">
												<el-option
													v-for="item in deviceTypeList"
													:key="item.id"
													:value="item.id"
													:label="item.typeName"
													:disabled="item.state === 20"
												/>
											</el-select>
										</el-form-item>
									</el-col>
									<el-col :span="24" class="mb20">
										<el-form-item label="运营商" prop="operatorId">
											<el-select v-model="state.queryForm.operatorId" filterable clearable placeholder="请选择运营商" :teleported="false">
												<el-option v-for="item in operatorList" :key="item.id" :value="item.id" :label="item.name" :disabled="item.state === 20" />
											</el-select>
										</el-form-item>
									</el-col>

									<el-col :span="24" class="mb20">
										<el-form-item label="创建时间" prop="createTime">
											<el-date-picker
												:end-placeholder="$t('syslog.inputEndPlaceholderTip')"
												:start-placeholder="$t('syslog.inputStartPlaceholderTip')"
												range-separator="To"
												type="datetimerange"
												v-model="state.queryForm.createTime"
												value-format="YYYY-MM-DD HH:mm:ss"
												:teleported="false"
											/>
										</el-form-item>
									</el-col>
								</el-row>
							</el-form>

							<template #reference>
								<el-button link type="primary"
									>高级搜索<el-icon><ArrowDown /></el-icon>
								</el-button>
							</template>
						</el-popover>
						<el-input v-model="state.queryForm.deviceModelName" :placeholder="'请输入设备型号'" clearable />
						<el-button type="primary" @click="getDataList(true)">搜索</el-button>
						<el-button icon="refresh" @click="resetQuery()"></el-button>
					</el-space>
					<div>
						<el-button icon="plus" type="success" class="ml10" @click="formDialogRef.openDialog()" v-auth="'platform_basicDeviceModel_add'">
							新 增
						</el-button>
					</div>
				</div>
			</el-row>
			<!-- <el-row>
				<div class="mb8" style="width: 100%">
					<right-toolbar
						v-model:showSearch="showSearch"
						:export="'platform_basicDeviceModel_export'"
						@exportExcel="exportExcel"
						class="ml10 mr20"
						style="float: right"
						@queryTable="getDataList"
					></right-toolbar>
				</div>
			</el-row> -->
			<el-table
				:data="state.dataList"
				v-loading="state.loading"
				border
				:cell-style="tableStyle.cellStyle"
				:header-cell-style="tableStyle.headerCellStyle"
				@sort-change="sortChangeHandle"
			>
				<el-table-column type="index" label="序号" width="60" />
				<el-table-column prop="deviceType" label="设备类型" show-overflow-tooltip />
				<el-table-column prop="deviceModelName" label="设备型号" show-overflow-tooltip />
				<el-table-column prop="communicationModelName" label="通讯模型" show-overflow-tooltip />
				<el-table-column prop="operatorName" label="运营商" show-overflow-tooltip />
				<el-table-column prop="status" label="状态" show-overflow-tooltip>
					<template #default="{ row }">
						<el-tag :type="row.state === 10 ? 'success' : 'danger'">{{ row.state === 10 ? '正常' : '已禁用' }}</el-tag>
					</template>
				</el-table-column>
				<el-table-column prop="createTime" label="创建时间" show-overflow-tooltip />
				<el-table-column label="操作" width="210">
					<template #default="scope">
						<el-button
							:type="scope.row.state === 10 ? 'danger' : 'success'"
							v-auth="'platform_basicDeviceModel_enableOrDisable'"
							@click="handleSwitchState(scope.row)"
							>{{ scope.row.state === 10 ? '禁用' : '启用' }}</el-button
						>
						<el-button icon="edit-pen" text type="primary" v-auth="'platform_basicDeviceModel_edit'" @click="formDialogRef.openDialog(scope.row.id)"
							>编辑</el-button
						>
						<el-button icon="delete" text type="danger" v-auth="'platform_basicDeviceModel_del'" @click="handleDelete([scope.row.id])"
							>删除</el-button
						>
					</template>
				</el-table-column>
			</el-table>
			<pagination @size-change="sizeChangeHandle" @current-change="currentChangeHandle" v-bind="state.pagination" />
		</div>

		<!-- 编辑、新增  -->
		<form-dialog ref="formDialogRef" @refresh="getDataList(false)" />
		<!-- 启用、禁用 -->
		<switch-form-dialog ref="switchFormDialogRef" :api="enableOrDisable" @refresh="getDataList(false)" />
	</div>
</template>

<script setup lang="ts" name="systemBasicDeviceModel">
import { BasicTableProps, useTable } from '/@/hooks/table';
import { fetchList, delObjs } from '/@/api/platform/basicDeviceModel';
import { useMessage, useMessageBox } from '/@/hooks/message';
import { fetchListAll } from '/@/api/platform/basicDeviceType';
import { fetchListAll as fetchOperatorList } from '/@/api/platform/basicOperatorType';
import { enableOrDisable } from '/@/api/platform/basicDeviceModel';
import { useDict } from '/@/hooks/dict';

// 引入组件
const FormDialog = defineAsyncComponent(() => import('./form.vue'));
const SwitchFormDialog = defineAsyncComponent(() => import('/@/components/EnableOrDisable/index.vue'));
// 定义查询字典

// 定义变量内容
const formDialogRef = ref();
const switchFormDialogRef = ref();
// 搜索变量
const queryRef = ref();
const showSearch = ref(true);
const deviceTypeList: any = ref([]);
const operatorList: any = ref([]);
// 多选变量
const selectObjs = ref([]) as any;
const multiple = ref(true);

const state: BasicTableProps = reactive<BasicTableProps>({
	queryForm: {},
	pageList: fetchList,
	descs: ['create_time'],
});

//  table hook
const { getDataList, currentChangeHandle, sizeChangeHandle, sortChangeHandle, downBlobFile, tableStyle } = useTable(state);

// 清空搜索条件
const resetQuery = () => {
	// 清空搜索条件
	queryRef.value?.resetFields();
	// 清空多选
	selectObjs.value = [];
	state.queryForm.deviceModelName = '';
	getDataList();
};

// 获取所有设备类型
const getDeviceTypes = () => {
	fetchListAll({ state: 20 }).then((res) => {
		deviceTypeList.value = res.data;
	});
};
getDeviceTypes();
const getOperatorList = () => {
	fetchOperatorList({ state: 10 }).then((res) => {
		operatorList.value = res.data;
	});
};
getOperatorList();
// 导出excel
const exportExcel = () => {
	downBlobFile('/platform/basicDeviceModel/export', Object.assign(state.queryForm, { ids: selectObjs }), 'basicDeviceModel.xlsx');
};

// 多选事件
const selectionChangHandle = (objs: { id: string }[]) => {
	selectObjs.value = objs.map(({ id }) => id);
	multiple.value = !objs.length;
};

// 删除操作
const handleDelete = async (ids: string[]) => {
	try {
		await useMessageBox().confirm('此操作将永久删除');
	} catch {
		return;
	}

	try {
		await delObjs(ids);
		getDataList();
		useMessage().success('删除成功');
	} catch (err: any) {
		useMessage().error(err.msg);
	}
};
// 启用禁用
const handleSwitchState = async (row: { id: string; state: number }) => {
	const text = row.state === 10 ? '禁用' : '启用';
	try {
		await useMessageBox().confirm(`您将对该设备型号进行${text}操作，确定继续吗？`, false);
	} catch {
		return;
	}
	unref(switchFormDialogRef).openDialog(row.id, row.state === 10 ? 20 : 10, `${text}设备型号`);
};
</script>
